package com.yvon.maple.service.base.feign;

import com.yvon.boot.common.result.ResponseData;
import com.yvon.maple.cloud.api.base.dto.FileInfoDTO;
import com.yvon.maple.cloud.api.base.feign.FileFeignClient;
import com.yvon.maple.cloud.web.data.ApiController;
import com.yvon.maple.constants.SystemConstant;
import com.yvon.maple.service.base.entity.FileInfo;
import com.yvon.maple.service.base.service.FileInfoService;
import com.yvon.maple.utils.DataConverter;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;
import java.util.Set;

import static com.yvon.maple.service.base.feign.FileFeignController.PATH;

/**
 * @author yvon
 * @since 2023-04-23
 */
@RestController
@RequestMapping(value = PATH)
public class FileFeignController extends ApiController {

	public static final String PATH = SystemConstant.Common.FEIGN_INSIDE_URL_PREFIX + "/files";

    @Autowired
    private FileInfoService fileInfoService;

	@GetMapping( "/id/{id}")
    public ResponseData<FileInfoDTO> getById(@PathVariable("id") Long id) {
        FileInfo fileInfo = fileInfoService.getById(id);
        FileInfoDTO fileInfoDTO = new FileInfoDTO();
        BeanUtils.copyProperties(fileInfo, fileInfoDTO);
        return success(fileInfoDTO);
    }

	@DeleteMapping("/id/{id}")
    public ResponseData<Boolean> deleteById(@PathVariable("id") Long id) {
        return success(fileInfoService.removeById(id));
    }

	@DeleteMapping
    public ResponseData<Boolean> deleteByIds(@RequestBody List<Long> ids) {
        return success(fileInfoService.removeByIds(ids));
    }


	@PostMapping("/incr")
    public ResponseData<Boolean> incrSave(@RequestParam(value = "fileIds") @NotNull Set<Long> fileIds, @RequestParam(value = "businessKey") @NotNull String businessKey) {
        return success(fileInfoService.incrSave(fileIds, businessKey));
    }

	@PostMapping("/cover")
    public ResponseData<Boolean> coverSave(@RequestParam(value = "fileIds") Set<Long> fileIds, @RequestParam(value = "businessKey") @NotNull String businessKey) {
        return success(fileInfoService.coverSave(fileIds, businessKey));
    }

	@GetMapping("/key/tempUrl")
    public ResponseData<List<String>> getTempUrlByKey(@RequestParam(value = "businessKeys") @NotNull Set<String> businessKeys, @RequestParam(value = "hours", required = false) Integer hours) {
        return success(fileInfoService.getTempUrlByBusiness(businessKeys, hours));
    }

	@GetMapping("/id/tempUrl")
    public ResponseData<List<String>> tempUrlByFileIds(@RequestParam(value = "fileIds") @NotNull Set<Long> fileIds, @RequestParam(value = "hours", required = false) Integer hours) {
        return success(fileInfoService.getTempUrl(fileIds, hours));
    }

	@GetMapping("/map/tempUrl")
    public ResponseData<Map<String, List<String>>> mapTempUrl(@RequestParam(value = "businessKeys") @NotNull Set<String> businessKeys, @RequestParam(value = "hours", required = false) Integer hours) {
        Map<String, List<String>> map = fileInfoService.tempUrlMap(businessKeys, hours);
        return success(map);
    }

	@GetMapping("/info")
    public ResponseData<List<FileInfoDTO>> fileInfo(@RequestParam(value = "businessKeys") @NotNull Set<String> businessKeys, @RequestParam(value = "hours", required = false) Integer hours) {
        List<FileInfo> fileInfoList = fileInfoService.getFileInfo(businessKeys, hours);
        return success(DataConverter.convert(fileInfoList, FileInfoDTO.class));
    }
}
